/*
 * Copyright 2009 Lukasz Wozniak
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *
 * http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package pl.sind.http;

import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;

import pl.sind.http.basic.SimpleJavaHttpConnector;

/**
 * Default connector factory, uses {@link HttpURLConnection} can use SOCKS or HTTP proxy w/o authentication.
 * @author Lukasz Wozniak
 *
 */
public class DefaultHttpConnectorFactory implements HttpConnectorFactory {

	private HttpProxyConfig proxyConfig;

	public HttpConnector getConnector() {
		Proxy p = null;
		if (proxyConfig != null) {
			Type type = null;
			switch (proxyConfig.getType()) {
			case HttpProxyConfig.SOCKS:
				type = Type.SOCKS;
				break;
			case HttpProxyConfig.HTTP:
				type = Type.HTTP;
				break;
			default:
				throw new IllegalArgumentException("Unknown proxy type "
						+ proxyConfig.getType());
			}
			p = new Proxy(type, new InetSocketAddress(proxyConfig.getHost(),
					proxyConfig.getPort()));

		}
		return new SimpleJavaHttpConnector(p);

	}

	public void setHttpProxyConfig(HttpProxyConfig proxyConfig) {
		this.proxyConfig = proxyConfig;
	}

}
